package YunNotes.servlet;


import YunNotes.allnotes.dao.PublicSquareAll;
import YunNotes.util.json.ChangeJson;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;


/**
 * 输出所有的公开笔记
 */
@WebServlet("/PublicSquareServlet")
public class PublicSquareServlet extends HttpServlet {


    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        // 响应参数格式设置
        response.setCharacterEncoding("UTF-8");
        response.setContentType("text/html;charset=UTF-8");

        //获取前端的请求的方法
        String requestMethod = request.getMethod();
        System.out.println("前端的请求方法：" + requestMethod);

        //向外传值,并将数据转化为json格式保存在一个字符串对象里
        ChangeJson changeJson = new ChangeJson();
        PublicSquareAll publicSquareAll = new PublicSquareAll();

        //将此字符串对象输出
        PrintWriter out = response.getWriter();

        //如果未获取到值则说明点击的用户广场，将所有公开的笔记输出（但不输出笔记内容）
        String str = changeJson.ListToJson(publicSquareAll.outputPublicSquaresAll());

        out.println(str);
        System.out.println(str);
        System.out.println("已将数据返回前端");

        out.flush();
        out.close();


    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request, response);
    }
}
